using FluentValidation;
using Zhaoxi.Zhihu.Core.QuestionAggregate.Specifications;
using Zhaoxi.Zhihu.SharedKerel.Messaging;
using Zhaoxi.Zhihu.SharedKerel.Result;
using Zhaoxi.Zhihu.UseCases.Common.Interface;

public record UpdateAnswerLikeCommand(int AnswerId, bool IsLike) : ICommand<Result>;

public class UpdateAnswerLikeCommandValidator : AbstractValidator<UpdateAnswerLikeCommand>
{
    public UpdateAnswerLikeCommandValidator()
    {
        RuleFor(command=> command.AnswerId).GreaterThan(0);
    }
}


public class UpdateAnswerLikeCommandHandler(IAnswerRepositoy answers, IUser user): ICommandHandler<UpdateAnswerLikeCommand, Result>
{
    public async Task<Result> Handle(UpdateAnswerLikeCommand request, CancellationToken cancellationToken)
    {
        var spec = new AnswerByIdWithLikeByUserIdSpec(request.AnswerId, user. Id !. Value);
        var answer = await answers.GetAnswerByIdWithLikeByUserIdAsync(spec, cancellationToken);
        if (answer == null) return Result.NotFound("回答不存在");
        
        var result = answer.UpdateLike(user.Id!.Value, request.IsLike);
        if (!result. IsSuccess) return result;
        await answers. SaveChangesAsync(cancellationToken);
        return result;
    }
}
